/* -*- Mode: C;  tab-width: 4;  indent-tabs-mode: dunto;  c-basic-offset: 4 -*-
*
* Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software;  you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation;  either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY;  without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program;  if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#include "loadinglabel.h"

LoadingLabel::LoadingLabel(QObject *parent)
{
    m_timer = new QTimer(this);
    m_timer->setInterval(100);
    connect(m_timer,&QTimer::timeout,this,&LoadingLabel::refreshIconNum);
    initGsettings();
}

LoadingLabel::~LoadingLabel()
{
    m_timer->deleteLater();
}

void LoadingLabel::setTimerStop()
{  
    m_timer->stop();
}

void LoadingLabel::setTimerStart()
{
    if (!m_timer->isActive())
        m_timer->start();
}

void LoadingLabel::setTimeReresh(int m)
{
    m_timer->setInterval(m);
}

void LoadingLabel::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.setPen(Qt::transparent);

//    ukccbluetoothconfig::loadSvg(QIcon::fromTheme("ukui-loading-"+QString("%1").arg(index)+"-symbolic").pixmap(this->width(),this->height()),
//                                 ukccbluetoothconfig::WHITE)
//    qWarning()<< Q_FUNC_INFO << "_themeIsBlack:" << _themeIsBlack << __LINE__;

    if (_themeIsBlack)
        painter.drawImage(0,0,
                      ukccbluetoothconfig::loadSvgImage(QIcon::fromTheme("ukui-loading-"+QString("%1").arg(index)+"-symbolic").pixmap(this->width(),this->height()),ukccbluetoothconfig::WHITE),
                      0,0,-1,-1,Qt::ColorMode_Mask);
    else
        painter.drawPixmap(0,0,this->width(),this->height(),QIcon::fromTheme("ukui-loading-"+QString("%1").arg(index)+"-symbolic").pixmap(this->width(),this->height()));

}

void LoadingLabel::refreshIconNum()
{
//    KyDebug() << QString::number(i,10);
    this->update();
    index++;
    if(index > 7 || index < 0)
        index = 0;

}

void LoadingLabel::initGsettings()
{
//    qWarning()<< Q_FUNC_INFO << __LINE__;
    if (QGSettings::isSchemaInstalled(GSETTING_UKUI_STYLE))
    {
        _mStyle_GSettings = new QGSettings(GSETTING_UKUI_STYLE);

        if(_mStyle_GSettings->get("styleName").toString() == "ukui-default" ||
           _mStyle_GSettings->get("style-name").toString() == "ukui-light")
            _themeIsBlack = false;
        else
            _themeIsBlack = true;

        //_mIconThemeName = _mStyle_GSettings->get("iconThemeName").toString();
    }

    connect(_mStyle_GSettings,&QGSettings::changed,this,&LoadingLabel::mStyle_GSettingsSlot);
}

void LoadingLabel::mStyle_GSettingsSlot(const QString &key)
{
//    qWarning()<< Q_FUNC_INFO << key << __LINE__;

    if ("styleName" == key || "style-name" == key)
    {
        if(_mStyle_GSettings->get("style-name").toString() == "ukui-default" ||
           _mStyle_GSettings->get("style-name").toString() == "ukui-light")
        {
            _themeIsBlack = false;
        }
        else
        {
            _themeIsBlack = true;
        }
    }
}
